package com.gaussian.property.app.controller;

import com.gaussian.property.app.service.DashboardService;
import com.gaussian.property.app.vo.dashboard.*;
import com.gaussian.property.module.biz.entity.AppUser;
import com.songqi.common.core.domain.R;
import com.songqi.common.core.validator.DateRegex;
import com.songqi.common.web.request.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jetbrains.annotations.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/10/3 17:57
 */

@Api(tags = "看板")
@RestController
@RequestMapping("dashboard/")
@Validated
public class DashboardController extends BaseController {

    @Resource
    private DashboardService dashboardService;

    @ApiOperation("0:项目监控警告弹框")
    @GetMapping("monitorAlert")
    public R<List<ProjectMonitor>> monitorAlert(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId)  {
        String today = getReportDate();
        AppUser appUser = (AppUser)getTokenUser();
        return success(dashboardService.getProjectMonitor(appUser.getAppUserId(), projectId, today));
    }


    @ApiOperation("1：项目报表监控建议")
    @GetMapping("reportMonitorSuggest")
    public R<List<ProjectModuleReportMonitor>> reportMonitorSuggest(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.reportMonitorSuggest(projectId, today));
    }

    @ApiOperation("1：列出所有指标趋势图表")
    @GetMapping("trends")
    public R<List<Trend>> trends(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                    @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                    @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month) {
        return success(dashboardService.getTrends(projectId, month));
    }



    @ApiOperation("2：数据总览-今日扣款统计。对应数据总览中的“今日扣款”字段")
    @GetMapping("todayDeduction")
    public R<Double> todayDeduction(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyDeduction(projectId, today));
    }

    @ApiOperation("2：数据总览-月度扣款统计，对应数据总览中的除“今日扣款”字段之外的字段")
    @GetMapping("monthlyDeduction")
    public R<MonthlyDeduction> monthlyDeduction(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                              @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                               @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyDeduction(projectId, month));
    }

    @ApiOperation("3：扣款明细-今日扣款明细。对应扣款明细的今日数据和今日扣款两列")
    @GetMapping("todayDeductionDetail")
    public R<DeductionDetail> todayDeductionDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyDeductionDetail(projectId, today));
    }

    @ApiOperation("3：扣款明细-本月扣款明细。对应扣款明细的本月数据和本月扣款两列")
    @GetMapping("monthlyDeductionDetail")
    public R<DeductionDetail> monthlyDeductionDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                   @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                   @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyDeductionDetail(projectId, month));
    }

    @ApiOperation("4：人员出勤-总览。今日考勤人数、缺岗人数、超龄人数统计")
    @GetMapping("todayCrewAttendanceCount")
    public R<CrewAttendanceCount> todayCrewAttendanceCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyCrewAttendanceCount(projectId, today));
    }

    @ApiOperation("4：人员出勤-总览。月度考勤人数、缺岗人数、超龄人数统计")
    @GetMapping("monthlyCrewAttendanceCount")
    public R<CrewAttendanceCount> monthlyCrewAttendanceCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                           @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                           @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyCrewAttendanceCount(projectId, month));
    }


    @ApiOperation("5：人员出勤-月度数据。列出每日出勤人数")
    @GetMapping("monthlyCrewAttendanceDetail")
    public R<List<DailyCrewAttendance>> monthlyCrewAttendanceDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                                    @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                                     @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyCrewAttendanceDetail(projectId, month));
    }



    @ApiOperation("6：人员管理-今日总览。怠工、串岗、脱岗、聚岗等人数统计")
    @GetMapping("todayCrewBehaviorCount")
    public R<CrewBehaviorCount> todayCrewBehaviorCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyCrewBehaviorCount(projectId, today));
    }


    @ApiOperation("6：人员管理-月度总览。怠工、串岗、脱岗、聚岗等人数统计")
    @GetMapping("monthlyCrewBehaviorCount")
    public R<CrewBehaviorCount> monthlyCrewBehaviorCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                             @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                             @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyCrewBehaviorCount(projectId, month));
    }

    @ApiOperation("7：人员管理-今日数据。列出所有员工怠工、串岗、脱岗、聚岗明细")
    @GetMapping("todayCrewBehaviorDetail")
    public R<List<CrewBehaviorDetail>> todayCrewBehaviorDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyCrewBehaviorDetail(projectId, today));
    }



    @ApiOperation("8：人员管理-月度数据。列出所有员工怠工、串岗、脱岗、聚岗明细")
    @GetMapping("monthlyCrewBehaviorDetail")
    public R<List<CrewBehaviorDetail>> monthlyCrewBehaviorDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                                 @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                                 @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyCrewBehaviorDetail(projectId, month));
    }

    @ApiOperation("9：人员管理-月度数据。列出每天的怠工、串岗、脱岗、聚岗等人数统计")
    @GetMapping("monthlyCrewBehaviorCountDetail")
    public R<List<DailyCrewBehavior>> monthlyCrewBehaviorCountDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                                 @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                                 @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyCrewBehaviorCountDetailList(projectId, month));
    }

    @ApiOperation("10：作业覆盖-总览。今日风险区域、不及格巡检数量")
    @GetMapping("todayJobCoverageCount")
    public R<JobCoverageCount> todayJobCoverageCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyJobCoverageCount(projectId, today));
    }

    @ApiOperation("10：作业覆盖-总览。月度风险区域、不及格巡检数量")
    @GetMapping("monthlyJobCoverageCount")
    public R<JobCoverageCount> monthlyJobCoverageCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                       @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                       @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyJobCoverageCount(projectId, month));
    }

    @ApiOperation("11：作业覆盖-月度数据。列出每日风险区域、不及格巡检数量")
    @GetMapping("monthlyJobCoverageDetail")
    public R<List<JobCoverageCount>> monthlyJobCoverageDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                              @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                              @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyJobCoverageDetail(projectId, month));
    }

    @ApiOperation("12：作业覆盖-月度数据(空间)。列出每日空间风险区域数量")
    @GetMapping("monthlySpaceRiskAreaCountDetail")
    public R<List<SpaceRiskAreaCount>>  monthlySpaceRiskAreaCountDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                              @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                              @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlySpaceRiskAreaCountDetail(projectId, month));
    }


    @ApiOperation("13：机器人-总览。今日日清工单、未达标机器人数量")
    @GetMapping("todayRobotCount")
    public R<RobotCount> todayRobotCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId){
        String today = getReportDate();
        return success(dashboardService.getDailyRobotCount(projectId, today));
    }

    @ApiOperation("13：机器人-总览。月度日清工单、未达标机器人数量")
    @GetMapping("monthlyRobotCount")
    public R<RobotCount> monthlyRobotCount(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                       @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                       @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyRobotCount(projectId, month));
    }

    @ApiOperation("14：机器人-月度数据。列出每日日清工单、未达标机器人数量")
    @GetMapping("monthlyRobotCountDetail")
    public R<List<RobotCount>> monthlyRobotCountDetail(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                              @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                              @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyRobotCountDetail(projectId, month));
    }

    @ApiOperation("15：机器人-月度数据（机器人清洁面积）。显示机器人每日清洁面积")
    @GetMapping("monthlyRobotCleanArea")
    public R<List<RobotCleanArea>> monthlyRobotCleanArea(@ApiParam("项目id") @Validated @NotBlank(message = "项目id不能为空") String projectId,
                                                               @ApiParam("月份。格式：yyyyMM") @Validated @NotBlank(message = "月份不能为空")
                                                       @DateRegex(format = "yyyyMM", message = "日期格式不正确") String month){
        return success(dashboardService.getMonthlyRobotCleanArea(projectId, month));
    }



    private String getReportDate() {
        return LocalDate.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }
}
